package com.t2t.top.framework.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogUtils {

    private final static LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

    /**
     * 修改指定类的日志级别
     */
    public static void changeLevel(String name, String level) {
        Logger logger = lc.getLogger(name);
        logger.setLevel(Level.toLevel(level));
        logger.debug("name:{},level:{}", name, level);
    }

    /**
     * 获取日志列表
     */
    public static List<Map> getLogs() {
        Map map = new HashMap();
        List<Logger> loggers = lc.getLoggerList();
        List<Map> list = new ArrayList();
        for (int i = 0; i < loggers.size(); i++) {
            Map item = new HashMap();
            Logger l = loggers.get(i);
            item.put("name", l.getName());
            item.put("level", l.getLevel() == null ? "" : MessageFormat.format("<font style='color:red'>{0}</font>", l.getLevel().toString()));
            list.add(item);
        }

        //this.getRequest().setAttribute("json", GsonUtils.toJson(log.getLoggerContext().getTurboFilterList()));
        //map.bind("birth", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lc.getBirthTime()));
        return list;
    }

    public static String getBirth() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lc.getBirthTime());
    }


}  